const BaseHandler = require('../base-handler');
const HTTP_VERBS = require('../../../constants').HTTP_VERBS;
const utils = require('../../../utils');
const async = require('async');

/**
 * Profile handler
 * 
 * @class ProfileHandler
 * @extends {BaseHandler}
 */
class FHTLiteResearchHandler extends BaseHandler {

    //TODO: remove form authentication

    get pattern() {
        return "/fht-lite/research";
    }

    doPOST(request, response) {
        var me = this;
        var currentData = {};
        currentData.user_agent = request.headers['user-agent'];
        currentData.remote_ip = utils.getRemoteIP(request);
        currentData.session_id = request.sessionID;
        currentData.http_referer = request.headers['referer'];
        currentData.send_frequency = Number(request.body.send_frquency);
        currentData.send_date = request.body.send_date;
        currentData.send_time_interval = request.body.send_time;
        currentData.increased_Fans = Number(request.body.increased_Fans);
        currentData.total_Fans = Number(request.body.total_Fans);

        async.waterfall([
            //首先将收集到的数据存入数据库
            (callback) => {
                var StatisticsResult = {};
                StatisticsResult.saveStatus = false;
                me.Controller.saveFHTLiteResearchData(currentData, (error, result) => {
                    if (error) {
                        return response.json({
                            success: false,
                            error: '存入数据失败'
                        });
                    } else {
                        if (result.errors == undefined) {
                            //先将loss_rate存入数据库
                            StatisticsResult.conclusionResult = {};
                            StatisticsResult.conclusionResult.loss_rate = `${result._doc.loss_rate}%`;
                            StatisticsResult.saveStatus = true;
                            callback(null, StatisticsResult);
                        }
                    }
                });
            },
            (StatisticsResult, callback) => {
                //然后将从数据库查询并计算出来的每条记录数，和占总数的百分比获取
                me.Controller.getFHTLiteResearchStatistics(StatisticsResult, (error, result) => {
                    if (error) {
                        //获取统计数据失败
                        return response.json({
                            success: false,
                            error: '获取统计数据失败'
                        });
                    };
                    callback(null, result);
                });
            },
            (statisticsResult, callback) => {
                //开始计算当前用户的排名, 评分, 丢失流量百分比,占总流量的百分比
                me.Controller.calculateUserResult(statisticsResult, (error, result) => {
                    if (error) {
                        return response.json({
                            success: false,
                            error: '计算当前用户结果失败'
                        });
                    };
                    response.json(result);
                })
            }
        ])

    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
    }
}

module.exports = FHTLiteResearchHandler;